package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbAddressVoService;
import com.dongdongshop.vo.TbAddressVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class TbAddressVoServiceImpl implements TbAddressVoService {

    //注入mapper
    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbProvincesMapper tbProvincesMapper;
    @Autowired
    private TbCitiesMapper tbCitiesMapper;
    @Autowired
    private TbAreasMapper tbAreasMapper;

    //查询订单地址数据
    @Override
    public List<TbAddressVo> showTbAddressVo(String username) {
        return tbAddressMapper.showTbAddressVo(username);
    }

    //获取省份地址
    @Override
    public List<TbProvinces> showTbProvinces() {
        return tbProvincesMapper.selectByExample(null);
    }

    //获取市区地址
    @Override
    public List<TbCities> showTbCities(String provinceid) {
        TbCitiesExample te = new TbCitiesExample();
        TbCitiesExample.Criteria criteria = te.createCriteria();
        criteria.andProvinceidEqualTo(provinceid);
        return tbCitiesMapper.selectByExample(te);
    }

    //获取县地址
    @Override
    public List<TbAreas> showTbAreas(String cityid) {
        TbAreasExample te = new TbAreasExample();
        TbAreasExample.Criteria criteria = te.createCriteria();
        criteria.andCityidEqualTo(cityid);
        return tbAreasMapper.selectByExample(te);
    }

    //添加地址
    @Override
    public void addTbAddress(TbAddress tbAddress) {
        tbAddressMapper.insertSelective(tbAddress);
    }

    //删除地址
    @Override
    public void deleteTbAddress(Long id) {
        tbAddressMapper.deleteByPrimaryKey(id);
    }

    //地址回显
    @Override
    public TbAddress echoTbAddress(Long id) {
        return tbAddressMapper.selectByPrimaryKey(id);
    }

    //修改地址
    @Override
    public void updateTbAddress(TbAddress tbAddress) {
        tbAddressMapper.updateByPrimaryKeySelective(tbAddress);
    }

    //设置为默认地址
    @Override
    public void zheweimoren(Long id,TbUser tbUser) {
        TbAddressExample te = new TbAddressExample();
        TbAddressExample.Criteria criteria = te.createCriteria();
        criteria.andUserIdEqualTo(tbUser.getUsername());
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(te);
        for (int i = 0; i < tbAddresses.size(); i++) {

            if (tbAddresses.get(i).getId() == id) {
                tbAddresses.get(i).setIsDefault("1");
                tbAddressMapper.updateByPrimaryKeySelective(tbAddresses.get(i));
            } else {
                tbAddresses.get(i).setIsDefault("0");
                tbAddressMapper.updateByPrimaryKeySelective(tbAddresses.get(i));
            }
        }
    }
}
